package com.coder.gaeblogger;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.coder.gaeblogger.domain.User;
import com.coder.gaeblogger.manager.UserManager;

@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		String username =  req.getParameter("username");
		String password =  req.getParameter("password");
		resp.setContentType("text/plain");
		
		if(username != null && password != null){
			UserManager userM = new UserManager();		
			User user = userM.checkUser(username, password);
			if(user == null){
				resp.getWriter().println("No user or password for provide info");
				//froword(req, resp, nextJSP);
				
				
			}else{
				resp.getWriter().println("login success");
				req.getSession().setAttribute("user", user);
				String nextJSP = "/admin/hello.jsp";
				froword(req, resp, nextJSP);
			}
			
		}else{
			resp.getWriter().println("No user or password");
			//froword(req, resp, nextJSP);
		}
		
		
		
		
		resp.getWriter().println("Wel Come GAE-Blog");
	}
	
	
	
	private void froword(HttpServletRequest req, HttpServletResponse resp, String nextJSP){
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		try {
			dispatcher.forward(req,resp);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
